/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentContact;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class EstablishmentContactListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"EstablishmentContactList.xls\"");
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setVerticalAlignment(1);
		HSSFRow r = sheet.createRow(0);
		r.createCell(0).setCellValue(new HSSFRichTextString("编号"));
		r.createCell(1).setCellValue(new HSSFRichTextString("名称"));
		r.createCell(2).setCellValue(new HSSFRichTextString("姓名"));
		r.createCell(3).setCellValue(new HSSFRichTextString("性别"));
		r.createCell(4).setCellValue(new HSSFRichTextString("职务"));
		r.createCell(5).setCellValue(new HSSFRichTextString("手机"));
		r.createCell(6).setCellValue(new HSSFRichTextString("Email"));
		r.createCell(7).setCellValue(new HSSFRichTextString("电话"));
		r.createCell(8).setCellValue(new HSSFRichTextString("QQ/MSN"));
		r.createCell(9).setCellValue(new HSSFRichTextString("备注"));
		List list = (List) model.get("list");
		long preEstId = 0L;
		int rowSpan = 0;

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			Establishment est = (Establishment) objs[0];
			EstablishmentContact ec = (EstablishmentContact) objs[1];
			r = sheet.createRow(os + 1);
			HSSFCell c = r.createCell(0);
			HSSFCell c1 = r.createCell(1);
			r.createCell(2).setCellValue(
					new HSSFRichTextString(ec.getContactName()));
			r.createCell(3).setCellValue(new HSSFRichTextString(ec.getSex()));
			r.createCell(4)
					.setCellValue(new HSSFRichTextString(ec.getPossie()));
			r.createCell(5)
					.setCellValue(new HSSFRichTextString(ec.getMobile()));
			r.createCell(6).setCellValue(new HSSFRichTextString(ec.getEmail()));
			r.createCell(7).setCellValue(new HSSFRichTextString(ec.getPhone()));
			r.createCell(8).setCellValue(new HSSFRichTextString(ec.getIm()));
			r.createCell(9).setCellValue(new HSSFRichTextString(ec.getMark()));
			if (est.getId() != preEstId) {
				if (rowSpan != 0) {
					sheet.addMergedRegion(new Region(os - rowSpan, 0, os, 0));
					sheet.addMergedRegion(new Region(os - rowSpan, 1, os, 1));
				}

				c.setCellValue(new HSSFRichTextString(est.getEstCode()));
				c.setCellStyle(cellStyle);
				c1.setCellValue(new HSSFRichTextString(est.getEstName()));
				c1.setCellStyle(cellStyle);
				rowSpan = 0;
				preEstId = est.getId();
			} else {
				++rowSpan;
			}

			if (os == list.size() - 1 && rowSpan != 0) {
				sheet.addMergedRegion(new Region(os + 1 - rowSpan, 0, os + 1, 0));
				sheet.addMergedRegion(new Region(os + 1 - rowSpan, 1, os + 1, 1));
			}
		}

		ServletOutputStream arg17 = response.getOutputStream();
		workbook.write(arg17);
		arg17.flush();
		arg17.close();
	}
}